package com.kstudio.videocapture.editvideo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.widget.ImageView;

import com.kstudio.videocapture.KVideoCapture;
import com.kstudio.videocapture.R;
import com.kstudio.videocapture.editimage.KImageEditActivity;
import com.kstudio.videocapture.editvideo.KVideoEditControl.KEditControlListener;
import com.kstudio.videocapture.editvideo.KVideoEditSaveEffect.KVideoEditSaveEffectListener;
import com.kstudio.videocapture.listimage.KImageListActivity;
import com.kstudio.videocapture.util.KLog;
import com.kstudio.videocapture.z.KFileListAsync;
import com.kstudio.videocapture.z.KFileListAsync.KVideoCaptureListener;

public class KVideoEditActivity extends Activity implements KEditControlListener, KVideoCaptureListener, KVideoEditSaveEffectListener {
    private final KLog log = new KLog("KVideoEdit");

    private Context mContext;

    private KVideoCapture mSnapshot;

    private String mInPath;
    private String mOutPath;

    private KVideoEditControl mEditControl;

    private KFileListAsync mVideoCaptureAsync;

    private ImageView               mEditAnimation;
    private AnimationSet            mFlash;
    private KVideoEditSaveEffect    mSaveEffect;

    private int  mSaveEffectCnt;
    private Rect mSaveEffectDstRect;
    private ArrayList<String> mSavedPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        log.i("onCreate");

        mContext = this;

        setContentView(R.layout.video_edit_activity);

        Intent intent = getIntent();
        if(intent != null) {
            mInPath = intent.getStringExtra("path");
        }
        mSnapshot = (KVideoCapture)getApplicationContext();
        mOutPath = mSnapshot.getStoragePath();

        mVideoCaptureAsync = new KFileListAsync(this);
        mFlash = null;
        mSaveEffectCnt = 0;
        mSavedPath = new ArrayList<String>();

        mEditControl = (KVideoEditControl) findViewById(R.id.video_edit_control);
        mEditControl.setOnEditControlListener(this);
        mEditControl.setVideoPath(mInPath);

        mEditAnimation = (ImageView) findViewById(R.id.video_edit_animation);
        mEditAnimation.setBackgroundColor(0xFFFFFFFF);
        mEditAnimation.setVisibility(View.GONE);

        mSaveEffect = (KVideoEditSaveEffect) findViewById(R.id.video_edit_save_effect);
        mSaveEffect.setSaveEffectListener(this);
        mSaveEffect.setVisibility(View.GONE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_filelist, menu);
        return true;
    }

    @Override
    public void onSingleSaveButtonClick(int time) {
        mSaveEffectCnt = 1;
        int[] t = new int[mSaveEffectCnt];
        t[0] = time;

        startSaveEffect(t);
    }

    @Override
    public void onMultiSaveButtonClick(int time) {
        mSaveEffectCnt = 10;
        int[] t = new int[mSaveEffectCnt];
        for(int i = 0; i < mSaveEffectCnt; i++)
            t[i] = time + i*1000;

        startSaveEffect(t);
    }

    @Override
    public void onRepositoryButtonClick() {
        if(mSavedPath.isEmpty()) {
            Intent intent = new Intent(mContext, KImageListActivity.class);
            startActivity(intent);
        } else if(mSavedPath.size() == 1) {
            String path = mSavedPath.get(0);
            Intent intent = new Intent(mContext, KImageEditActivity.class);
            intent.putExtra("path", path);
            startActivity(intent);
        } else {

        }
    }

    @Override
    public void onProgressUpdate() {
    }

    @Override
    public void onProgressUpdate(String path, Bitmap b) {
        shinyAnimation();
        mSaveEffectCnt--;
        mSaveEffect.setSaveEffectImage(path, b);
        b.recycle();
    }

    @Override
    public void onResult(int result) {
    }

    @Override
    public void onEffectUpdate(String path, Bitmap bm) {
        Bitmap b = Bitmap.createScaledBitmap(bm, mSaveEffectDstRect.right - mSaveEffectDstRect.left, mSaveEffectDstRect.bottom - mSaveEffectDstRect.top, false);

        mSavedPath.add(path);

        mEditControl.setRepositoryImage(Bitmap.createBitmap(b));

        if(mSaveEffectCnt <= 0) stopSaveEffect();
    }

    private void startSaveEffect(int time[]) {
        mSaveEffectDstRect = mEditControl.getEffectDstRect();

        mVideoCaptureAsync.start(KFileListAsync.COMMAND_SAVE_FRAME, mInPath, mOutPath, time);

        mEditControl.setGroupEnabled(false);

        mSaveEffect.setVisibility(View.VISIBLE);
        mSaveEffect.startSaveEffect(mSaveEffectDstRect, 2000);
    }

    private void stopSaveEffect() {
        mVideoCaptureAsync.stop();

        mEditControl.setGroupEnabled(true);

        mSaveEffect.setVisibility(View.GONE);
    }

    private void shinyAnimation() {
        if(mFlash != null) {
            mFlash.cancel();
        }

        mFlash = new AnimationSet(true);

        Animation a1 = new AlphaAnimation(0.0f, 0.9f);
        a1.setDuration(200);
        mFlash.addAnimation(a1);

        Animation a2 = new AlphaAnimation(0.8f, 1.0f);
        a2.setDuration(300);
        mFlash.addAnimation(a2);

        Animation a3 = new AlphaAnimation(1.0f, 0.0f);
        a3.setDuration(200);
        mFlash.addAnimation(a3);

        mFlash.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                log.i("onAnimationStart");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                log.i("onAnimationEnd");
                mEditAnimation.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                log.i("onAnimationRepeat");
            }
        });

        mEditAnimation.setVisibility(View.VISIBLE);
        mEditControl.setGroupEnabled(false);
        mEditAnimation.startAnimation(mFlash);
    }

}
